package pkg

import (
	"context"
	"encoding/json"

	"github.com/redis/go-redis/v9"
)

type MessageH struct {
	client *redis.Client
	ctx    context.Context
}

func (m *MessageH) MessageHandler(msg Message) error {
	data, err := json.Marshal(&msg)
	if err != nil {
		return err
	}

	for k, v := range data {
		err := m.client.HSet(m.ctx, msg.Id, k, v).Err()
		if err != nil {
			panic(err)
		}
	}
	// userSession := m.client.HGetAll(m.ctx, msg.Id).Val()
	// fmt.Println(userSession, "O_o")

	return nil
}

func NewMessageHandler(c *redis.Client, ctx context.Context) *MessageH {
	return &MessageH{
		client: c,
		ctx:    ctx,
	}
}
